//==========================================================================================
//
//		OpenNETCF.Samples.SerialCSharp.frmSettings
//		Copyright (c) 2003-2009, OpenNETCF Consulting, LLC
//      http://www.opennetcf.com
//
//      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
//      associated documentation files (the "Software"), to deal in the Software without restriction, 
//      including without limitation the rights to use, copy, modify, merge, publish, distribute, 
//      sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
//      furnished to do so, subject to the following conditions:
//
//      The above copyright notice and this permission notice shall be included in all copies or 
//      substantial portions of the Software.
// 
//      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
//      BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
//      NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
//      DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
//      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
//
//==========================================================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using OpenNETCF.IO.Serial;


namespace OpenNETCF.Samples.SerialCSharp
{
	/// <summary>
	/// Summary description for frmSettings.
	/// </summary>
	public class frmSettings : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.RadioButton rdoCOM1;
		private System.Windows.Forms.RadioButton rdoCOM2;
		private System.Windows.Forms.RadioButton rdoCOM3;
		private System.Windows.Forms.Button btnDone;
		private System.Windows.Forms.RadioButton rdoCOM4;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox cboSpeed;
		private System.Windows.Forms.ComboBox cboParity;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ComboBox cboStopBits;
	
		private DetailedPortSettings portSettings;

		private struct ListItem
		{
			public ListItem(string LabelString, uint Constant)
			{
				this.LabelString = LabelString;
				this.Constant = Constant;
			}

			public override string ToString()
			{
				return LabelString;
			}

			public string LabelString;
			public uint Constant;
		}

		public frmSettings()
		{
			int i = -1;

				InitializeComponent();
			
			// each list item will hold the label and the constant
			cboSpeed.Items.Add(new ListItem("2400", (uint)BaudRates.CBR_2400));
			cboSpeed.Items.Add(new ListItem("4800", (uint)BaudRates.CBR_4800));
			cboSpeed.Items.Add(new ListItem("9600", (uint)BaudRates.CBR_9600));
			cboSpeed.Items.Add(new ListItem("14400", (uint)BaudRates.CBR_14400));
			i = cboSpeed.Items.Add(new ListItem("19200", (uint)BaudRates.CBR_19200));
			cboSpeed.SelectedIndex = i;
			cboSpeed.Items.Add(new ListItem("57600", (uint)BaudRates.CBR_57600));

			i = cboParity.Items.Add(new ListItem("None", (uint)Parity.none));
			cboParity.SelectedIndex = i;
			cboParity.Items.Add(new ListItem("Odd", (uint)Parity.odd));
			cboParity.Items.Add(new ListItem("Even", (uint)Parity.even));
			cboParity.Items.Add(new ListItem("Mark", (uint)Parity.mark));
			cboParity.Items.Add(new ListItem("Space", (uint)Parity.space));

			i = cboStopBits.Items.Add(new ListItem("1", (uint)StopBits.one));
			cboStopBits.SelectedIndex = i;
			cboStopBits.Items.Add(new ListItem("1.5", (uint)StopBits.onePointFive));
			cboStopBits.Items.Add(new ListItem("2", (uint)StopBits.two));
		}

		frmSettings(DetailedPortSettings PortSetings)
		{
			portSettings = PortSetings;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.rdoCOM4 = new System.Windows.Forms.RadioButton();
			this.rdoCOM3 = new System.Windows.Forms.RadioButton();
			this.rdoCOM2 = new System.Windows.Forms.RadioButton();
			this.rdoCOM1 = new System.Windows.Forms.RadioButton();
			this.label1 = new System.Windows.Forms.Label();
			this.btnDone = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.cboSpeed = new System.Windows.Forms.ComboBox();
			this.cboParity = new System.Windows.Forms.ComboBox();
			this.label3 = new System.Windows.Forms.Label();
			this.cboStopBits = new System.Windows.Forms.ComboBox();
			this.label4 = new System.Windows.Forms.Label();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.rdoCOM4);
			this.panel1.Controls.Add(this.rdoCOM3);
			this.panel1.Controls.Add(this.rdoCOM2);
			this.panel1.Controls.Add(this.rdoCOM1);
			this.panel1.Controls.Add(this.label1);
			this.panel1.Location = new System.Drawing.Point(8, 8);
			this.panel1.Size = new System.Drawing.Size(220, 60);
			// 
			// rdoCOM4
			// 
			this.rdoCOM4.Location = new System.Drawing.Point(148, 36);
			this.rdoCOM4.Size = new System.Drawing.Size(60, 16);
			this.rdoCOM4.Text = "COM4";
			this.rdoCOM4.CheckedChanged += new System.EventHandler(this.rdoCOM4_CheckedChanged);
			// 
			// rdoCOM3
			// 
			this.rdoCOM3.Location = new System.Drawing.Point(148, 12);
			this.rdoCOM3.Size = new System.Drawing.Size(60, 16);
			this.rdoCOM3.Text = "COM3";
			this.rdoCOM3.CheckedChanged += new System.EventHandler(this.rdoCOM3_CheckedChanged);
			// 
			// rdoCOM2
			// 
			this.rdoCOM2.Checked = true;
			this.rdoCOM2.Location = new System.Drawing.Point(76, 36);
			this.rdoCOM2.Size = new System.Drawing.Size(60, 16);
			this.rdoCOM2.Text = "COM2";
			this.rdoCOM2.CheckedChanged += new System.EventHandler(this.rdoCOM2_CheckedChanged);
			// 
			// rdoCOM1
			// 
			this.rdoCOM1.Location = new System.Drawing.Point(76, 12);
			this.rdoCOM1.Size = new System.Drawing.Size(60, 16);
			this.rdoCOM1.Text = "COM1";
			this.rdoCOM1.CheckedChanged += new System.EventHandler(this.rdoCOM1_CheckedChanged);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
			this.label1.Location = new System.Drawing.Point(12, 24);
			this.label1.Size = new System.Drawing.Size(44, 24);
			this.label1.Text = "Port:";
			// 
			// btnDone
			// 
			this.btnDone.Location = new System.Drawing.Point(140, 240);
			this.btnDone.Size = new System.Drawing.Size(92, 24);
			this.btnDone.Text = "Done";
			this.btnDone.Click += new System.EventHandler(this.btnDone_Click);
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
			this.label2.Location = new System.Drawing.Point(12, 80);
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.Text = "Speed:";
			// 
			// cboSpeed
			// 
			this.cboSpeed.Location = new System.Drawing.Point(80, 76);
			this.cboSpeed.Size = new System.Drawing.Size(148, 22);
			this.cboSpeed.SelectedIndexChanged += new System.EventHandler(this.cboSpeed_SelectedIndexChanged);
			// 
			// cboParity
			// 
			this.cboParity.Location = new System.Drawing.Point(80, 104);
			this.cboParity.Size = new System.Drawing.Size(148, 22);
			this.cboParity.SelectedIndexChanged += new System.EventHandler(this.cboParity_SelectedIndexChanged);
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
			this.label3.Location = new System.Drawing.Point(8, 108);
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.Text = "Parity:";
			// 
			// cboStopBits
			// 
			this.cboStopBits.Location = new System.Drawing.Point(80, 132);
			this.cboStopBits.Size = new System.Drawing.Size(148, 22);
			this.cboStopBits.SelectedIndexChanged += new System.EventHandler(this.cboStopBits_SelectedIndexChanged);
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
			this.label4.Location = new System.Drawing.Point(8, 136);
			this.label4.Size = new System.Drawing.Size(68, 16);
			this.label4.Text = "Stop Bits:";
			// 
			// frmSettings
			// 
			this.ControlBox = false;
			this.Controls.Add(this.cboStopBits);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.cboParity);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.cboSpeed);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.btnDone);
			this.Controls.Add(this.panel1);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Text = "frmSettings";

		}
		#endregion

		protected override void OnClosing(CancelEventArgs e)
		{
			// don't close the form, just hide it
			e.Cancel = true;
			this.Visible = false;
		}

		private void btnDone_Click(object sender, System.EventArgs e)
		{
			this.Visible = false;
		}

		private void rdoCOM1_CheckedChanged(object sender, System.EventArgs e)
		{
			frmMain.port.PortName = "COM1:";
		}

		private void rdoCOM2_CheckedChanged(object sender, System.EventArgs e)
		{
			frmMain.port.PortName = "COM2:";
		}

		private void rdoCOM3_CheckedChanged(object sender, System.EventArgs e)
		{
			frmMain.port.PortName = "COM3:";		
		}

		private void rdoCOM4_CheckedChanged(object sender, System.EventArgs e)
		{
			frmMain.port.PortName = "COM4:";		
		}

		private void cboSpeed_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			frmMain.port.Settings.BaudRate = (BaudRates)((ListItem)((ComboBox)sender).SelectedItem).Constant;
		}

		private void cboParity_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			frmMain.port.Settings.Parity = (Parity)((ListItem)((ComboBox)sender).SelectedItem).Constant;
		}

		private void cboStopBits_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			frmMain.port.Settings.StopBits = (StopBits)((ListItem)((ComboBox)sender).SelectedItem).Constant;
		}
	}
}
